package com.um.springboot.starter.service.impl;


import com.um.springboot.starter.properties.HshProperties;
import com.um.springboot.starter.service.CacheService;
import com.um.springboot.starter.service.CommonService;
import com.um.springboot.starter.service.TokenService;
import com.um.springboot.starter.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author renjunwei
 * @date 2021/12/15
 * @copyright 广州优匠科技有限公司
 */
@Service
public class TokenServiceImpl implements TokenService {


    @Autowired
    private HshProperties hshProperties;

    @Autowired
    private CommonService commonService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 生成token
     *
     * @param userFieldValue
     * @return
     */
    @Override
    public String generate(String userFieldValue) {

        Map tokenMap = commonService.queryEntity(hshProperties.getToken().getTableName(), hshProperties.getToken().getUserField(), userFieldValue);
        if (tokenMap == null) {
            tokenMap = new Hashtable();
        }

        String oldTokenStr = StringUtil.getTrim(tokenMap.get(hshProperties.getToken().getKey()));
        String tokenStr = MD5Util.MD5(userFieldValue + DateUtil.getDatetimeString());

        tokenMap.put(hshProperties.getToken().getUserField(), userFieldValue);
        tokenMap.put(hshProperties.getToken().getKey(), tokenStr);
        tokenMap.put("create_time", DateUtil.getDatetimeString());
        boolean result = commonService.save(hshProperties.getToken().getTableName(), tokenMap);

        if(!result){
            return "";
        }

        if(hshProperties.getToken().getType().equals("redis")){
            Long expire = hshProperties.getToken().getExpire();

            if(expire > 0){
                redisTemplate.opsForValue().set(tokenStr, userFieldValue, expire, TimeUnit.MINUTES);
            }else{
                redisTemplate.opsForValue().set(tokenStr, userFieldValue);
            }
            if(null != oldTokenStr && !oldTokenStr.equals("")){
                redisTemplate.delete(oldTokenStr);
            }
        }

        return tokenStr;
    }

    /**
     * 移除token
     *
     * @param userFieldValue
     * @return
     */
    @Override
    public boolean remove(String userFieldValue) {
        List<Map> tokenMapList = commonService.queryList(hshProperties.getToken().getTableName(), new Object[]{hshProperties.getToken().getUserField()}, new Object[]{userFieldValue});
        if (tokenMapList.size() == 0) {
            return true;
        }

        if(hshProperties.getToken().getType().equals("redis")){
            for (Map tokenMap : tokenMapList) {
                redisTemplate.delete(StringUtil.getTrim(tokenMap.get(hshProperties.getToken().getKey())));
            }
        }

        return commonService.deleteByProperties(hshProperties.getToken().getTableName(), new String[]{hshProperties.getToken().getUserField()}, new Object[]{userFieldValue});
    }
}
